'use strict';

const Controller = require('egg').Controller;
const Error = require('../../class/error');

class ArticleController extends Controller {

  /**
   * @description 新文章页面
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async new() {
    const ctx = this.ctx;
    const articleType = await this.service.articleType.list();
    ctx.locals = {
      articleTypes: articleType,
    }
    await ctx.render('admin/article/add_or_edit.html');
  }

  /**
   * @description 编辑
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async edit() {
    const ctx = this.ctx;
    const app = this.app;
    const article = await this.service.article.selectById(+ctx.query.articleId);
    const articleType = await this.service.articleType.list();
    const operations = await this.service.operationLog.getPreList(
      app.config.operationType.target.ARTICLE,
      app.config.operationType.type.VIEW,
      +ctx.query.articleId
    );
    ctx.locals = {
      article: article,
      articleTypes: articleType,
      operations: operations,
    }
    await ctx.render('admin/article/add_or_edit.html');
  }

  /**
   * @description 文章列表
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async list() { 
    const ctx = this.ctx;
    const app = this.app;
    const query = ctx.query;

    let params = {
      pageSize: +query.pageSize || app.config.pageBean.pageSize,
      pageNo: +query.pageNo || 1,
      orderBy: query.orderBy || 'create_time',
      order: query.order || 'DESC',
      keyword: query.keyword || '',
      publish: query.publish,
      type: query.type
    }
    let where = {
      $or: [
        {
          title: {
            $like: '%' + params.keyword + '%'
          }
        },
        {
          keywords: {
            $like: '%' + params.keyword + '%'
          }
        },
        {
          author: {
            $like: '%' + params.keyword + '%'
          }
        }
      ],
    };
    if (params.publish)
      where.publish = params.publish;
    if (params.type)
      where.type = params.type;
    const data = await this.service.article.list(
      params.pageNo,
      params.pageSize,
      params.orderBy,
      params.order,
      where
    );
    params.totalSize = data.count;
    params.totalPage = data.count == 0 ? 1 : Math.ceil(data.count / params.pageSize);

    //分类
    const types = await this.service.articleType.list();
    ctx.locals = {
      params: params,
      list: data.rows,
      types: types
    }

    await ctx.render('admin/article/list.html');

  }

  //json

  /**
   * @description 删除json
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async deleteJson() {
    const ctx = this.ctx;
    const query = ctx.query;
    try {
      await this.service.article.delete(query.articleId);
      ctx.body = Error.SUCCESS;
    } catch (e) {
      ctx.body = Error.UNKOWN;
    }
  }

  /**
   * @description 发布的togglejson
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async togglePublishJson() {
    const ctx = this.ctx;
    const query = ctx.query;
    const data = await this.service.article.selectById(query.articleId);
    let values = {};
    let errMsg = "";
    if (data.publish == 0) {
      values.publish = 1;
      errMsg = "发布成功";
    } else {
      values.publish = 0;
      errMsg = "取消发布成功";
      if (!values.publish_time)
        values.publish_time = new Date();
    }
    try {
      await this.service.article.togglePublish(query.articleId, values);
      ctx.body = Error.getErrorMsg(Error.SUCCESS.errCode, errMsg);
    } catch (e) {
      ctx.body = Error.UNKOWN;
      throw e;
    }
  }

  /**
   * @description 编辑 post
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleController
   */
  async addOrEditJson() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    const values = {
      author: body.author,
      content: body.content,
      file_ids: +body.file_ids,
      img: body.img,
      keywords: body.keywords,
      sub_title: body.sub_title,
      title: body.title,
      type: +body.type,
      modify_user_id: ctx.session.userInfo.id,
      modify_time: new Date(),
      attachments_notation: body.attachments_notation,
      relative_article: body.relative_article,
    }
    let article = {};
    if (body.id) {
      values.id = +body.id;
      await this.service.article.update(values);
      article = await this.service.article.selectById(values.id);
    } else {
      values.publish = 1;
      values.create_time = new Date();
      values.publish_time = new Date();
      article = await this.service.article.add(values);
    }
    ctx.body = article;
  }
}

module.exports = ArticleController;